<?php
namespace app\common\controller;
use \think\Request;
use \think\Session;
class Base extends \think\Controller
{
    public function _initialize()
    {
    		/**定义了3个常量，获取当前运行的module、controller、action的名字*/
		define('MODULE_NAME',strtolower(Request::instance()->module()));
		define('CONTROLLER_NAME',strtolower(Request::instance()->controller()));
		define('ACTION_NAME',strtolower(Request::instance()->action()));
		
		//判断是否登录
		$user_id = session("user_id");
		
		if(empty($user_id)){
			Session::clear();
//			跳转到登录页
			$this->error('你还未登录',url('login/login/crm_login'));
		}else{
			/*
			 * 头部制度
			 */
			$head_department_list = db("department")->field('name,id')->select();
			array_shift($head_department_list);
			
			/*
			 * 判断权限及设置对应的session
			 */
	        $res = permission();

	        if(!$res[1]){
	            $this->error("你的权限不够");
            }
	        if(in_array("common_show_all_data",$res[0])){
	        	session('common_show_all_data',true);
	        }else{
	        	session('common_show_all_data',false);
	        }
			$user_name = session("user_name");
			$this->assign([
				"username"=>$user_name
                ,"MODULE_NAME"=>MODULE_NAME
				,"CONTROLLER_NAME"=>CONTROLLER_NAME
				,"ACTION_NAME"=>ACTION_NAME
				,"head_department_list"=>$head_department_list
			]);
		}
		
	}

    /**
     * 定义上传图片类
     * @return \think\response\Json
     */
    public function upload_image(){
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            return json([
                'status'=>1,
                'msg'=>"上传成功",
                'img_path'=> DS.'uploads'.DS.$info->getSaveName()
            ]);
        }else{
            return json([
                'status'=>0,
                'msg'=> $file->getError()
            ]);
        }
    }





}
